var page = require('webpage').create();
//page.zoomFactor=1.5
var d = new Date(), month, date;
d.setDate(d.getDate() - 2);
month = d.getMonth() + 1;
if(month < 10) month = '0' + month;
date = d.getDate();
if(date < 10) date = '0' + date;
var day2ago = "" + d.getFullYear() + month + date;

d = new Date();
d.setDate(d.getDate() - 8);
month = d.getMonth() + 1;
if(month < 10) month = '0' + month;
date = d.getDate();
if(date < 10) date = '0' + date;
var day8ago = "" + d.getFullYear() + month + date;
page.viewportSize = {width: 1600, height: 1024};
page.open('https://accounts.google.com/ServiceLogin?service=analytics&passive=1209600&continue=https://analytics.google.com/analytics/web/%23my-reports%2FyfS0VwKjT2qBlk8mowtKzw%2Fa53591733w90901564p94589714%2F%3F_u.date00%3D'+day8ago+'%26_u.date01%3D'+day2ago+'%2F&followup=https://analytics.google.com/analytics/web/', function() {
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
        page.evaluate(function() {
            $('#Email').val('warerwang@gmail.com');
            $('#next').click();
        });
        window.setTimeout(function () {
            page.evaluate(function() {
                $('#Passwd').val('Hq7FiH#D9!BV');
                $('#signIn').click();
            });
            window.setTimeout(function () {
                // page.render('ga.png');
                page.render('ga.pdf');

                page.viewportSize = {width: 1600, height: 1600};
                page.open('https://analytics.google.com/analytics/web/?hl=zh-CN&pli=1#report/conversions-goal-funnel/a53591733w90901564p94589714/%3F_u.date00%3D'+day8ago+'%26_u.date01%3D'+day2ago+'%26_.goalOption%3D7/',function(){
                    window.setTimeout(function () {
                        // page.render('ga.png');
                        page.render('ga_funnel.pdf');
                        phantom.exit();
                    }, 10000 );
                });

            }, 30000 );
        }, 5000 );
    });
});